<?php
#### NAVIGATION MODEL ##########################################################
class NavigationModel {

	#### CONSTRUCTOR ###########################################################
	function NavigationModel() {
		Base::getThis();

		// Default table to perform actions on
		$this->default_table = $this->db->table['navigation'];
	}

	#### GET ITEMS #############################################################
	function getItems($limit = FALSE) {

		// Build our SQL depending on supplied arguments
		$this->db->select($this->default_table);
		$this->db->limit($limit);

		// Perform the query
		$this->db->query() or sql_error(__FILE__, __LINE__);

		// Put our result rows into an array ready to be returned to a foreach
		return $this->db->result();
	}

	#### GET ITEMS #############################################################
	function getItemsByPosition($limit = FALSE) {

		// Build our SQL depending on supplied arguments
		$this->db->select($this->default_table);
		$this->db->order('position');
		$this->db->limit($limit);

		// Perform the query
		$this->db->query() or sql_error(__FILE__, __LINE__);

		// Put our result rows into an array ready to be returned to a foreach
		return $this->db->result();
	}

	#### GET ITEMS #############################################################
	function getCachedItemsByPosition($limit = FALSE) {

		// Build our SQL depending on supplied arguments
		$this->db->select($this->default_table);
		$this->db->order('position');
		$this->db->limit($limit);

		// Put our result rows into an array ready to be returned to a foreach
		return $this->db->cache('navigation');
	}

	#### GET ITEM BY ID ########################################################
	function getItemByID($id) {

		// Our query SQL
		$this->db->select($this->default_table).
		$this->db->where('id = '.$id);

		// Perform the query
		$this->db->query() or sql_error(__FILE__, __LINE__);
		return $this->db->fetchArray();
	}

	#### ITEM COUNT ############################################################
	function getItemCount() {

		// Build our SQL query
		$this->db->count($this->default_table);

		// Perform the query
		$this->db->query() or sql_error(__FILE__, __LINE__);
		list($count) = $this->db->fetchRow();

		return $count;
	}

	#### INSERT ITEM ###########################################################
	function insertItem() {

		// Build our SQL query
		$this->db->insert($this->default_table, $this->post);

		// Perform the query
		$this->db->query() or sql_error(__FILE__, __LINE__);
		$this->db->deleteCache('navigation');
	}

	#### UPDATE ITEM ###########################################################
	function updateItem($id) {

		// Build our SQL query
		$this->db->update($this->default_table, $this->post);
		$this->db->where('id='.$id);

		// Perform the query
		$this->db->query() or sql_error(__FILE__, __LINE__);
		$this->db->deleteCache('navigation');
	}

	#### UPDATE ITEM POSITION ##################################################
	function updateItemPosition($key, $value) {

		$fields['position'] = $key;

		// Build our SQL query
		$this->db->update($this->default_table, $fields);
		$this->db->where('id = '.$value);

		// Perform the query
		$this->db->query() or sql_error(__FILE__, __LINE__);
		$this->db->deleteCache('navigation');
	}

	#### DELETE ITEM ###########################################################
	function deleteItem($id) {

		// Build our SQL query
		$this->db->delete($this->default_table);
		$this->db->where('id = '.$id);
		$this->db->limit(1);

		// Perform the query
		$this->db->query() or sql_error(__FILE__, __LINE__);
		$this->db->deleteCache('navigation');
	}

	#### UPDATE ITEM STATUS ####################################################
	function updateItemStatus($id) {

		$item = $this->getItemByID($id);

		$fields['disabled'] = ($item['disabled'] ? 0 : 1);

		// Build our SQL query
		$this->db->update($this->default_table, $fields);
		$this->db->where('id='.$id);

		// Perform the query
		$this->db->query() or sql_error(__FILE__, __LINE__);
		$this->db->deleteCache('navigation');
	}
}
?>